<?php
require_once(SERVER_ROOT . '/daos/' . 'UsuarioDAO.php');
require_once(SERVER_ROOT . '/controllers/' . 'admin.php');
require_once(SERVER_ROOT . '/authenticate/' . 'Authenticator.php');

class GerenteController extends AdminController {
	private $usuarioDAO;
	private $secretaria;

	public function __construct()
	{
		$this->usuarioDAO = new UsuarioDAO();
		$this->secretaria =  Authenticator::getUser();
	}

	public function listAllByType($p){
		$page = $p['page'];
		$limit = $p['rows'];
		$sidx = $p['sidx'];
		$sord = $p['sord'];
		if(!$sidx) $sidx = 1;
		$count = $this->usuarioDAO->countByOffice($p['type'],$this->secretaria->id_escritorio);
		$total_pages = ($count > 0 ? ceil($count/$limit): $total_pages = 0);
		if($page > $total_pages) $page = $total_pages;
		$start = $limit*$page-$limit;
		if($start < 0) $start = 0;
		$result=  $this->usuarioDAO->findAllByOffice($p['type'], $sidx, $sord, $start, $limit,$this->secretaria->id_escritorio);
		$response->page = $page;
		$response->total = $total_pages;
		$response->records = $count;
		for ($i = 0; $i < count($result); $i++) {
			$row = $result[$i];
			$response->rows[$i]['id']=$row['id_usuario'];
			$response->rows[$i]['cell']=array($row['id_usuario'],$row['id_escritorio'],$row['nome'], $row['endereco'], $row['cidade'], $row['cep'], $row['telefone_fixo'], $row['telefone_movel'], $row['email'], $row['estado'],$row['rg'],$row['cpf'],$row['data_nascimento'], $row['status'], '');
		}
		echo json_encode($response);
	}
}
